/**
 * Created by Administrator on 2016/9/6.
 */

var sqlClient = require('../lib/mysql/mysql');
var withdrawDao = module.exports;
var moment = require('moment');

/*
 * 提现列表
 *
 * */
withdrawDao.getWithdrawList = function(page,size,cb){
    var fields = [
        'withdraw_id',
        'withdraw_order_no',
        'rb_withdraw.add_date',
        'rb_withdraw.status',
        'rb_withdraw.last_modify_time',
        'rb_withdraw.user_id',
        'nick_name',
        'money',
        'wx'

    ];

    var sql = '';
    var sqlSelect = 'select ' + fields.join(',');
    var sqlFrom = ' from rb_withdraw ';
    var sqlJoin = 'left join rb_users on rb_users.user_id = rb_withdraw.user_id ';
    var sqlCondition = 'where state = 1';
    var sqlSort = ' order by rb_withdraw.add_date desc limit ?,?'
    sql = sqlSelect + sqlFrom + sqlJoin + sqlCondition + sqlSort;
    console.log(sql);
    sqlClient.query(sql,[parseInt((page - 1) * size), parseInt(size)], function(err, data){
        if(err){
            console.log(err);
            return cb && cb(err, null);
        }
        console.log(data);
        return cb && cb(null, data);
    })
}

/*
 *
 * 提现列表的数量
 * */
withdrawDao.getWithdrawCount = function(cb){
    var sql = '';
    var sqlSelect = 'select count(withdraw_id) as counts ';
    var sqlFrom = 'from rb_withdraw ';
    var sqlCondition = ' where state = 1';
    sql = sqlSelect + sqlFrom + sqlCondition ;
    console.log(sql);
    sqlClient.query(sql,null, function(err, data){
        if(err){
            console.log(err);
            return cb && cb(err, null);
        }
        return cb && cb(null, data[0].counts);
    })
}

/*
* 修改提现订单的状态
*
*
* */
withdrawDao.updateStatus = function(withdrawId,cb){
    var sql = '';
    var sqlUpdate = 'update rb_withdraw ';
    var sqlSet = 'set status = 2 ';
    var sqlCondition = 'where withdraw_id = ' + withdrawId;
    var sql = sqlUpdate + sqlSet + sqlCondition;
    console.log(sql);
    sqlClient.query(sql,null, function(err, data){
        if(err){
            console.log(err);
            return cb && cb(err, null);
        }
        return cb && cb(null, data);
    })

}


